package com.jiang.jixiang.network;


import android.content.Intent;
import android.net.Uri;

import com.jiang.jixiang.R;
import com.jiang.jixiang.tools.PubDiaUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.ws.universal.tools.Constants;
import com.ws.universal.tools.base.ManageActivity;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.utils.NetWorkUtils;
import com.ws.universal.tools.utils.StringUtils;
import com.ws.universal.tools.utils.TimeDateUtils;
import com.ws.universal.tools.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author:ws
 * @date: 2021/8/9 18:31
 * @desc 使用示例
 */
public class okHttpModel {
    private static int responseCode = 200;
    private String locat = "";

    //添加 header
    public static HttpHeaders getHttpHeaders() {

        HttpHeaders headers = new HttpHeaders();

        headers.put("openid", CacheUtils.getString(Constants.token));
        headers.put("client", "app");
        headers.put("time", StringUtils.getString(TimeDateUtils.getMilliSecond()));

        return headers;
    }


    public static void get(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {
        if (!NetWorkUtils.isAvailable()) {
            ToastUtils.showShort("无网络连接", 300);
            return;
        }

        OkGo.<String>get(StringUtils.getString(url, "?lang=", CacheUtils.getString(Constants.langCode))).params(map).headers(getHttpHeaders()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (listener != null && response != null) {
                    if (response != null && !StringUtils.isEmpty(response.body())) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            int code = object.optInt("code");
                            String msg = object.optString("msg");
                            CommonalityModel commonality = new CommonalityModel();
                            commonality.setStatusCode(code);
                            commonality.setMessage(msg);
                            if (responseCode == code) {
                                listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                            } else {
                                listener.onFail(object, ids, commonality);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            listener.onError(ids, e);
                            //LogcatUtils.e("ws  get catch  getMethod listener.onError()");
                        }
                    }
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (listener != null && response != null && response.getException() != null) {
                    listener.onError(ids, (Exception) response.getException());
                }
            }
        });
    }


    public static void postJson(String url, Map<String, Object> map, final int ids, final NetWorkListener listener) {
        if (!NetWorkUtils.isAvailable()) {
            ToastUtils.showShort("无网络连接", 300);
            return;
        }
        JSONObject jsonObject = new JSONObject(map);
        OkGo.<String>post(StringUtils.getString(url, "?lang=", CacheUtils.getString(Constants.langCode)))//
                .headers(getHttpHeaders())//
                //.params("param1", "paramValue1")//  这里不要使用params，upJson 与 params 是互斥的，只有 upJson 的数据会被上传
                .upJson(jsonObject)//
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (listener != null && response != null) {
                            if (response != null && !StringUtils.isEmpty(response.body())) {
                                try {
                                    JSONObject object = new JSONObject(response.body());
                                    int code = object.optInt("code");
                                    String msg = object.optString("msg");
                                    CommonalityModel commonality = new CommonalityModel();
                                    commonality.setStatusCode(code);
                                    commonality.setMessage(msg);
                                    if (responseCode == code) {
                                        listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                                    } else {
                                        listener.onFail(object, ids, commonality);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    listener.onError(ids, e);
                                    //LogcatUtils.e("ws post  catch  getMethod listener.onError()");
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (listener != null && response != null && response.getException() != null) {
                            listener.onError(ids, (Exception) response.getException());

                        }
                    }
                });


    }


    public static void post(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {
        if (!NetWorkUtils.isAvailable()) {
            ToastUtils.showShort("无网络连接", 300);
            return;
        }
        OkGo.<String>post(StringUtils.getString(url, "?lang=", CacheUtils.getString(Constants.langCode))).params(map).headers(getHttpHeaders()).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (listener != null && response != null) {
                    if (response != null && !StringUtils.isEmpty(response.body())) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            int code = object.optInt("code");
                            String msg = object.optString("msg");
                            CommonalityModel commonality = new CommonalityModel();
                            commonality.setStatusCode(code);
                            commonality.setMessage(msg);
                            if (responseCode == code) {
                                listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                            } else {
                                listener.onFail(object, ids, commonality);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            listener.onError(ids, e);
                            //LogcatUtils.e("ws post  catch  getMethod listener.onError()");
                        }
                    }
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (listener != null && response != null && response.getException() != null) {
                    listener.onError(ids, (Exception) response.getException());
                }
            }
        });
    }


    public static void postUpLoadFile(String url, ArrayList<File> files, final int ids, final NetWorkListener listener) {
        if (!NetWorkUtils.isAvailable()) {
            ToastUtils.showShort("无网络连接", 300);
            return;
        }
        OkGo.<String>post(StringUtils.getString(url, "?lang=", CacheUtils.getString(Constants.langCode))).headers(getHttpHeaders()).addFileParams("file", files).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                if (listener != null && response != null) {
                    if (response != null && !StringUtils.isEmpty(response.body())) {
                        try {
                            JSONObject object = new JSONObject(response.body());
                            int code = object.optInt("code");
                            String msg = object.optString("msg");
                            CommonalityModel commonality = new CommonalityModel();
                            commonality.setStatusCode(code);
                            commonality.setMessage(msg);
                            if (responseCode == code) {
                                listener.onSucceed(object, ids, commonality);//请求成功后通过接口返回数据
                            } else {
                                listener.onFail(object, ids, commonality);

                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                            listener.onError(ids, e);
                            //LogcatUtils.e("ws post  catch  getMethod listener.onError()");
                        }
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (listener != null && response != null && response.getException() != null) {
                    listener.onError(ids, (Exception) response.getException());
                }
            }
        });
    }

    private static void upDateApp(String apkUrl, String content, boolean isCompulsory) {
        PubDiaUtils.getInstance().showOneBtnDialog(ManageActivity.getInstance().getCurrentActivity(), ManageActivity.getInstance().getCurrentActivity().getString(R.string.new_version_discovered), content, ManageActivity.getInstance().getCurrentActivity().getString(R.string.update_now), isCompulsory, new PubDiaUtils.PublicDiaologCallback() {
            @Override
            public void onConfirm() {
                Uri uri = Uri.parse(apkUrl);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                ManageActivity.getInstance().getCurrentActivity().startActivity(intent);

            }

            @Override
            public void onCancel() {

            }
        });
    }

    // get rest 提交
    /*public static void getRest(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {


    }


    public static void put(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {


    }


    public static void putRest(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {


    }


    public static void Delete(String url, Map<String, String> map, final int ids, final NetWorkListener listener) {


    }*/


}
